package cn.hanyx.coder.design.pattern.structural.proxy.demo02;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * MyInvocationHandler
 *
 * @author hanyx
 * @date 2020/09/02
 */
public class MyInvocationHandler implements InvocationHandler {
    private Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Invoking sayHello");
        Object result = method.invoke(target, args);
        System.out.println("Invoking sayHello");
        return result;
    }

    public Object newProxyInstance(ClassLoader classLoader, Class<?>[] classes) {
        return Proxy.newProxyInstance(classLoader, classes, this);
    }


}
